package dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.JDBCException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.util.JDBCExceptionReporter;

import dao.utils.GenericHibernateDAO;
import domain.Clientes;
import domain.Proveedor;

public class ProveedorDAOImpl extends GenericHibernateDAO implements
		ProveedorDAO {

	public ProveedorDAOImpl(Session s) {
		super(s);
	}

	@SuppressWarnings("unchecked")
	public List<Proveedor> findAll() {
		Query q = getSession().createQuery("From Proveedor");
		return q.list();
	}

	@SuppressWarnings("unchecked")
	public boolean deleteProov(int id, Proveedor proov) {
		try {
			Query q = getSession().createQuery("select count(comp.idCompra) from Compra as comp, Proveedor as pro where pro.idProveedor = ?")
					.setInteger(0, id);
			Long numProov = (Long) q.uniqueResult();
			if (numProov >= 1) {
				return false;
			} else {
				getSession().delete(proov);
				return true;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	public boolean guardarProveedor(String nombre, Proveedor proveedor){
		Proveedor prov = (Proveedor) getSession().createQuery("from Proveedor as pro where pro.nombre = ?").setString(0, nombre).uniqueResult();
		if(prov !=  null){
			return false;
		}else{
			getSession().save(proveedor);
			return true;
		}
	}
	

}
